<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Spring Transaction &amp;nbsp;&amp;nbsp;Spring的事务机制是用统一的机制来处理不同数据访问计数的事务处理.它提供了一个PlatformTransactionManager接口,不同的数据访问技术使用不同的接口实现.    数据访问技术 接口实现    JDBC DataSourceTransactionManager   JPA JpaTransactionManager">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Data 事务&amp;缓存">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/01/2016-08-1-Spring-data-transaction/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="Spring Transaction &amp;nbsp;&amp;nbsp;Spring的事务机制是用统一的机制来处理不同数据访问计数的事务处理.它提供了一个PlatformTransactionManager接口,不同的数据访问技术使用不同的接口实现.    数据访问技术 接口实现    JDBC DataSourceTransactionManager   JPA JpaTransactionManager">
<meta property="og:locale">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6ldwfwuexj20ol0k5afd.jpg">
<meta property="article:published_time" content="2016-08-01T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6ldwfwuexj20ol0k5afd.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/01/2016-08-1-Spring-data-transaction/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>Spring Data 事务&缓存 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Transaction"><span class="nav-number">1.</span> <span class="nav-text">Spring Transaction</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E5%BC%8F%E4%BA%8B%E5%8A%A1"><span class="nav-number">1.1.</span> <span class="nav-text">声明式事务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Data-JPA%E6%94%AF%E6%8C%81"><span class="nav-number">1.2.</span> <span class="nav-text">Spring Data JPA支持</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81"><span class="nav-number">1.3.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Cache"><span class="nav-number">2.</span> <span class="nav-text">Spring Cache</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E5%BC%8F%E7%BC%93%E5%AD%98"><span class="nav-number">2.1.</span> <span class="nav-text">声明式缓存</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Example"><span class="nav-number">2.2.</span> <span class="nav-text">Example</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81-1"><span class="nav-number">2.3.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#end"><span class="nav-number">3.</span> <span class="nav-text">end</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/01/2016-08-1-Spring-data-transaction/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring Data 事务&缓存
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-08-01 18:00:00" itemprop="dateCreated datePublished" datetime="2016-08-01T18:00:00+08:00">2016-08-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="Spring-Transaction"><a href="#Spring-Transaction" class="headerlink" title="Spring Transaction"></a>Spring Transaction</h3><hr>
<p>&nbsp;&nbsp;Spring的事务机制是用统一的机制来处理不同数据访问计数的事务处理.它提供了一个<strong>PlatformTransactionManager</strong>接口,不同的数据访问技术使用不同的接口实现.</p>
<table>
<thead>
<tr>
<th>数据访问技术</th>
<th>接口实现</th>
</tr>
</thead>
<tbody><tr>
<td>JDBC</td>
<td>DataSourceTransactionManager</td>
</tr>
<tr>
<td>JPA</td>
<td>JpaTransactionManager</td>
</tr>
<tr>
<td>Hibernate</td>
<td>HibernateTransactionManager</td>
</tr>
<tr>
<td>JDO</td>
<td>JdoTransactionManager</td>
</tr>
<tr>
<td>分布式事务</td>
<td>JtaTransactionManager</td>
</tr>
</tbody></table>
<h4 id="声明式事务"><a href="#声明式事务" class="headerlink" title="声明式事务"></a>声明式事务</h4><hr>
<p>&nbsp;&nbsp;Spring使用<code>@Transactional</code>注解在方法上表明事务支持.被注解的方法在被调用时,会开启一个新的事务,当方法无异常完成提交后.Spring会提交事务.</p>
<p>&nbsp;&nbsp;<code>@Transactional</code>注解也可以用在类上,表明这个类下的所有方法都有事务支持.如果类和方法都使用了<code>@Transactional</code>,则使用方法上的注解覆盖类级别注解.</p>
<p>&nbsp;&nbsp;<code>@Transactional</code>注解是基于AOP的实现操作.</p>
<p>&nbsp;&nbsp;注意:<code>@Transactional</code>注解是由Spring提供的,而不是来自javax.transaction.</p>
<p>&nbsp;&nbsp;Spring提供<code>@EnableTransactionManagerment</code>注解在配置类上开启声明式事务,Spring容器会自动扫描带有注解<code>@Transactional</code>的类和方法.</p>
<h4 id="Spring-Data-JPA支持"><a href="#Spring-Data-JPA支持" class="headerlink" title="Spring Data JPA支持"></a>Spring Data JPA支持</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA对所有方法默认开启了事务支持,查询类事务默认启用readOnly-true属性.</p>
<h4 id="Spring-Boot支持"><a href="#Spring-Boot支持" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<p>&nbsp;&nbsp;Spring Boot会自动配置事务管理器.</p>
<ul>
<li>当使用JDBC时,Spring Boot会自动配置DataSourceTransactionManager.</li>
<li>当使用JPA时,Spring Boot会自动配置JpaTransactionManager.</li>
</ul>
<p>&nbsp;&nbsp;在Spring Boot中,不需要显式开启<code>@EnableTransactionManagement</code>注解.</p>
<h3 id="Spring-Cache"><a href="#Spring-Cache" class="headerlink" title="Spring Cache"></a>Spring Cache</h3><hr>
<p>&nbsp;&nbsp;Spring提供了CacheManager和Cache接口用来统一各种不同的数据缓存技术.</p>
<ul>
<li>CacheManager是各种缓存技术抽象接口.</li>
<li>Cache接口包含各种缓存操作.</li>
</ul>
<table>
<thead>
<tr>
<th>CacheManager</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>SimpleCacheManager</td>
<td>使用简单的Collection存储缓存,主要用于测试.</td>
</tr>
<tr>
<td>NoOpCacheManager</td>
<td>不会实际存储缓存.</td>
</tr>
<tr>
<td>EhCacheCacheManager</td>
<td>使用EhCache缓存技术.</td>
</tr>
<tr>
<td>ConcurrentMapCacheManager</td>
<td>使用ConcurrentMap存储缓存.</td>
</tr>
<tr>
<td>GuavaCacheManager</td>
<td>使用Google Guava的GuavaCache缓存技术.</td>
</tr>
<tr>
<td>HazelcastCacheManager</td>
<td>使用Hazelcast缓存技术.</td>
</tr>
<tr>
<td>JCacheCacheManager</td>
<td>支持JCache(JSR-107)规范的实现作为缓存技术.</td>
</tr>
<tr>
<td>RedisCacheManager</td>
<td>使用Redis作为缓存技术.</td>
</tr>
</tbody></table>
<p><strong>&nbsp;&nbsp;不管使用什么缓存技术,都需要注册一个该实现的CacheManager的Bean.</strong></p>
<h4 id="声明式缓存"><a href="#声明式缓存" class="headerlink" title="声明式缓存"></a>声明式缓存</h4><hr>
<p>&nbsp;&nbsp;Spring提供了以下注解用来声明式缓存.它与<code>@Transactional</code>注解一样是基于AOP操作的实现.</p>
<table>
<thead>
<tr>
<th>Annotation</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>@CachePut</td>
<td>不管什么情况,都会把方法的返回值存入缓存中.@CachePut的属性与@Cacheable保持一致.</td>
</tr>
<tr>
<td>@Cacheable</td>
<td>Spring会先查看缓存中是否存有数据,如果有,则直接返回缓存数据,如果没有,则将调用方法的返回值存入缓存中.</td>
</tr>
<tr>
<td>@Caching</td>
<td>可以通过@Caching注解组合多个注解策略在一个方法上.</td>
</tr>
<tr>
<td>@CacheEvict</td>
<td>将一条或多条缓存数据从缓存中删除.</td>
</tr>
</tbody></table>
<p><strong>&nbsp;&nbsp;开启声明式缓存需要在配置类上使用注解<code>@EnableCaching</code></strong></p>
<h4 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h4><hr>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6ldwfwuexj20ol0k5afd.jpg"></p>
<h4 id="Spring-Boot支持-1"><a href="#Spring-Boot支持-1" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<p>&nbsp;&nbsp;Spring Boot自动配置了CacheManager的各种实现,默认情况下使用的是ConcurrentMapCacheManager.支持以<code>spring.cache</code>为前缀的属性来配置缓存相关的配置.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">spring.cache.type&#x3D; #缓存技术的类型,可选ehcache,guava,simple,none,generic,hazelcast,infinispan,jcache,redis.</span><br><span class="line">spring.cache.cache-name&#x3D;#程序启动时创建缓存名称</span><br><span class="line">spring.cache.ehcache.config&#x3D;#ehcache配置文件的地址.</span><br><span class="line">spring.cache.hazelcast.config&#x3D;#hazelcast配置文件的地址.</span><br><span class="line">spring.cache.infinispan.config&#x3D;#infinispan配置文件的地址.</span><br><span class="line">spring.cache.jcache.config&#x3D;#jcache配置文件的地址.</span><br><span class="line">spring.cache.jcache.provider&#x3D;#当多个jcache实现在类路径中的时候,指定jcache实现.</span><br><span class="line">spring.cache.guava.spec&#x3D;# guava specs</span><br></pre></td></tr></table></figure>
<p><strong>&nbsp;&nbsp;使用Spring Boot只需要导入相关缓存技术的依赖,并在配置类使用<code>@EnableCaching</code>注解开启缓存支持即可.</strong></p>
<h3 id="end"><a href="#end" class="headerlink" title="end"></a>end</h3><blockquote>
<p>资料参考于 JavaEE开发的颠覆者: Spring Boot实战</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/Spring/" rel="tag"># Spring</a>
              <a href="/yuwanzi.io/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/07/31/2016-07-31-Spring-data/" rel="prev" title="Spring数据访问方案-Spring Data">
                  <i class="fa fa-chevron-left"></i> Spring数据访问方案-Spring Data
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/08/02/2016-08-2-Spring-security/" rel="next" title="Spring Security安全控制">
                  Spring Security安全控制 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
